package ntd.util.frame.javacv.ai_fuming;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.cron.CronUtil;
import com.modular.extra.ai.FactoryAiOpt;
import ntd.tool.ToolCron;
import ntd.util.UtilCatch;
import ntd.util.frame.javacv.action.bean.VisionModelDo;
import ntd.util.frame.javacv.remote.AiVisionRemote;
import ntd.util.frame.javacv.tool.ToolFtpSync;
import ntd.util.util.ToolDuration;

import java.util.ArrayList;
import java.util.List;

import static ntd.util.frame.javacv.ComVisionApi.initAiRemote;

/**
 * 文件拷贝 - 拷贝云端结果到本地
 */
public class AiFileSync {

    public static void main(String[] args) {
        // AI 分析结果同步本地任务
        CronUtil.schedule("15 * * * * *", (Runnable) AiFileSync::fileSyncDown);

        ToolCron.initTask();
    }

    public static void fileSyncDown() {
        List<VisionModelDo> visionModelDos = FactoryAiOpt.OPT_VISION_MODEL.cacheListAll();
        List<String> paths = new ArrayList<>();
        paths.addAll(CollStreamUtil.toList(visionModelDos, VisionModelDo::getPathImg));
        paths.addAll(CollStreamUtil.toList(visionModelDos, VisionModelDo::getPathJson));
        paths.addAll(CollStreamUtil.toList(visionModelDos, VisionModelDo::getPathVideo));
        paths.addAll(CollStreamUtil.toList(visionModelDos, VisionModelDo::getPathCsv));

        for (AiVisionRemote aiVisionRemote : initAiRemote()) {
            ToolFtpSync toolFtpSync = aiVisionRemote.getFtpSync();

            ToolDuration.duration(() -> {
//                CountDownLatch countDownLatchUp = ThreadUtil.newCountDownLatch(paths.size());
                for (String path : paths) {
                    UtilCatch.tryCatchNoRes(() -> toolFtpSync.downloadSync("/" + path));
//                    ThreadUtil.execAsync(() -> {});
                }
//                UtilCatch.tryCatchNoRes(countDownLatchUp::await);
            }, "文件拷贝");
        }
    }
}
